package com.leenmvc.core.utils;

import com.leenmvc.core.security.dao.core.SysTipDao;
import com.leenmvc.core.utils.spring.SpringContextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 提示语工具类获取提示语
 */
public class TipUtil {

    private static SysTipDao sysTipDao = null;

    private static Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");

    public static String getTip(String identification, Object... params) {
        if (sysTipDao == null) {
            sysTipDao = SpringContextUtils.getBean(SysTipDao.class);
        }

        // 如果identification匹配到中文那么直接返回
        Matcher matcher = pattern.matcher(identification);
        if(matcher.find()) {
            return identification;
        }
        String tip = sysTipDao.getTip(identification);
        if(tip == null) return identification;
        StringBuffer realtip = new StringBuffer(tip);
        for(int i = 0; i < params.length; i++) {
            int num = tip.indexOf("%s");
            if(num == -1) return realtip.toString();
            realtip.replace(num, num + 2, "");
            realtip.insert(num, params[i]);
        }
        return realtip.toString();
    }

}
